#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""KIEL data format

Copyright (c) 2009-2014 Christian T. Steigies <steigies@physik.uni-kiel.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

This module offers functions to read NM data in ASCII format as used in KIEL.

KIEL data availability
1h data since:
1957-07-01 C and O data only, no U or P data
1964-09-01 C, O, U and P data

1min data since:
1971-02-01 C, O and P data
"""

__author__ = "Christian T. Steigies <steigies@physik.uni-kiel.de>"
__license__ = "GPL License"

## --------------------------------------------------------------------------
import os

from nmdb.station.read_cosray import read_1m, read_1h
from nmdb.tools.datetool import valid_date

kiel_station = ["kiel"]


## --------------------------------------------------------------------------
def read_rt(options, station, data_type, year, month, day):
    """read realtime data

    options     verbose, dryrun, ...
    station     station short name, must be valid station
    data_type   "1m", "1h", "env", "meta"
    year        integer (1950-9999), may not be None
    month       integer (1-12), if None, read month 1-12
    day         integer (1-31), if None, read day 1-31

    return list of values in nmdb format
    """

    if station in kiel_station:
        pass
    else:
        raise ValueError  # wrong station

    value = []
    # "/home/kermes/steigies/.dosemu/drives/c/Inetpub/ftproot/CosRay!"
    data_path = '~/data/cosray'
    data_path = os.path.expanduser(data_path)

    if station in kiel_station:
        pressure = [950., 1050.]  # 982 - 1028
        countrate = [50., 1000.]
    else:
        raise ValueError  # wrong station

    if options.verbose > 0:
        print "read_kiel:", year, month, day

    if valid_date(year, month, day):
        if data_type in ["1h"]:
            value = read_1h(data_path, station, pressure, countrate, year, month, day)
        elif data_type in ["1m", "ori"]:
            value = read_1m(data_path, station, pressure, countrate, year, month, day)
        elif data_type in ["env", "meta"]:
            value = []
        else:
            raise ValueError  # illegal data_type

    return value

## --------------------------------------------------------------------------
def read_rev(options, station, data_type, year, month, day):
    """read revised data

    options     verbose, dryrun, ...
    station     station short name, must be valid station
    data_type   "1m", "1h", "env", "meta"
    year        integer (1950-9999), may not be None
    month       integer (1-12), if None, read month 1-12
    day         integer (1-31), if None, read day 1-31

    return list of values in nmdb format
    """

    if station in kiel_station:
        pass
    else:
        raise ValueError  # wrong station

    if data_type in ["1h"]:
        value = []
    elif data_type in ["1m"]:
        value = []
    elif data_type in ["env", "meta"]:
        value = []
    else:
        raise ValueError  # illegal data_type

    return value

## --------------------------------------------------------------------------
def main():
    import optparse
    parser = optparse.OptionParser("usage: %prog [options]")
    parser.add_option("-v", dest="verbose", default=0, action="count",
            help="increment output verbosity; may be specified multiple times")
    (options, args) = parser.parse_args()

    print "This is the KIEL_ASCII module for NMDB."

    station = "kiel"
    data_type="1h"
    #data_type="1m"
    year = 2014
    month = 10
    day = 10

    #data = read_rev(options, station, data_type, year, month, day)
    data = read_rt(options, station, data_type, year, month, day)
    for line in data:
        print line

## --------------------------------------------------------------------------
if __name__ == '__main__':
    main()
